#include<bits/stdc++.h>
using namespace std;
int n;
int a[200001];
int cnt[1000001];
int turn(string s){
    int ans=0;
    for(int i=1;i<s.size();++i){
        for(int j=i-1;j>=0;--j){
            if(s[i]==s[j]){
                ans+=a[i+1]*(a[i+1]==a[j+1]);
                break;
            }
        }
    }
    return ans;
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;++i) cin>>a[i];
        if(n<=15){
            stack<pair<int,string>> s;
            s.push(make_pair(0,""));
            int ans=0;
            while(!s.empty()){
                pair<int,string> copy=s.top();
                s.pop();
                if(copy.first==n+1){
                    ans=max(ans,turn(copy.second));
                    continue;
                }
                s.push(make_pair(copy.first+1,copy.second+'0'));
                s.push(make_pair(copy.first+1,copy.second+'1'));
            }
            cout<<ans<<"\n";
            continue;
        }
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=n;++i){
            ++cnt[a[i]];
        }
        long long ans=0;
        for(int i=1;i<=1000000;++i){
            if(cnt[i]>1) ans+=(cnt[i]-1)*i;
        }
        cout<<ans<<"\n";
    }
}